package c.m.d.b.rest.mapper;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public final class RestExceptionMapper<E extends Exception> implements ExceptionMapper<E>
{
    public RestExceptionMapper()
    {
    }

    public Response.Status getStatus(int errorCode)
    {
        switch (errorCode)
        {
            case 400:
                return Response.Status.BAD_REQUEST;
            case 403:
                return Response.Status.FORBIDDEN;
            case 404:
                return Response.Status.NOT_FOUND;
            case 409:
                return Response.Status.CONFLICT;
            case 500:
                return Response.Status.INTERNAL_SERVER_ERROR;
            default:
                throw new AssertionError("handled all cases");
        }
    }

    @Override
    public Response toResponse(E exception)
    {
        ErrorInfo info = new ErrorInfo(exception);
        return Response.status(getStatus(info.getErrorCode())).entity(info).build();
    }
}

